#coding=utf-8
__author__ = 'Meteor'

from base.webelement import Base
from selenium.webdriver.common.by import By

class Repost_page(Base):

    def clcik_create_new(self):
        '''
        点击新增仓库按钮
        :return: 
        '''
        self.click((By.CSS_SELECTOR,"#app > section > section > main > div:nth-child(3) > div > div > div > div > div.table-operator > button.ant-btn.ant-btn-primary"))


    def input_secth_repost_name(self,name):
        '''
        仓库名称搜索框输入
        :param name: 要搜索的仓库名称
        :return: 
        '''
        self.send_keys((By.CSS_SELECTOR,"#app > section > section > main > div:nth-child(3) > div > div > div > div > div.table-page-search-wrapper > form > div > div:nth-child(1) > div > div.ant-col.ant-col-18.ant-col-offset-1.ant-form-item-control-wrapper > div > span > input"),name)

    def input_secth_desc_info(self,key_word):
        '''
        输入查询时的描述信息
        :param key_word: 描述信息
        :return: 
        '''
        self.send_keys((By.CSS_SELECTOR,"#app > section > section > main > div:nth-child(3) > div > div > div > div > div.table-page-search-wrapper > form > div > div:nth-child(2) > div > div.ant-col.ant-col-18.ant-col-offset-1.ant-form-item-control-wrapper > div > span > input"),key_word)

    def click_secth_button(self):
        ''''
        点击搜索按钮
        '''
        self.click((By.CSS_SELECTOR,"#app > section > section > main > div:nth-child(3) > div > div > div > div > div.table-page-search-wrapper > form > div > span > div > button.ant-btn.ant-btn-primary"))

    def click_reset(self):
        '''
        点击重置按钮
        :return: 
        '''
        self.click((By.CSS_SELECTOR,"#app > section > section > main > div:nth-child(3) > div > div > div > div > div.table-page-search-wrapper > form > div > span > div > button:nth-child(2)"))


    def select_repost(self, *repost_name,select_all=None):
        '''
        批量选择仓库，或全部选择
        :param select_all: 是否全选的参数，默认不全选。不为None，则表示全选
        :param repost_name: 如果选择多个仓库，则传入多个仓库名称，名称之间用逗号隔开
        :return: 
        '''

        if select_all:
            self.click((By.CSS_SELECTOR,'#app > section > section > main > div:nth-child(3) > div > div > div > div > div:nth-child(3) > div > div > div > div > div > div > div.ant-table-header.ant-table-hide-scrollbar > table > thead > tr > th.ant-table-selection-column > span > div > span.ant-table-column-title > div > label > span'))
            # self.click((By.XPATH,'//*[@id="app"]/section/section/main/div[2]/div/div/div/div/div[3]/div/div/div/div/div/div/div[1]/table/thead/tr/th[1]/span/div/span[1]/div/label/span/input'))
        # 定位所有的仓库名称元素
        reposts = self.find_elements((By.XPATH, '//*[@id="app"]/section/section/main/div[2]/div/div/div/div/div[3]/div/div/div/div/div/div/div[2]/table/tbody/tr/td[3]'))
        for name in repost_name:
            # 定义一个计数器，记录当前仓库在第几行
            count = 0
            for repost in reposts:
                count += 1
                if name == repost.text:
                    # 确定要定位的仓库的点击框的xpath路径
                    repost_click_boxs = '//*[@id="app"]/section/section/main/div[2]/div/div/div/div/div[3]/div/div/div/div/div/div/div[2]/table/tbody/tr[%d]/td[1]/span/label/span' %count

                    self.click((By.XPATH,repost_click_boxs))

    def delet_reposts(self):
        '''
        点击批量删除
        :return: 
        '''
        self.click((By.CSS_SELECTOR,"#app > section > section > main > div:nth-child(3) > div > div > div > div > div.table-operator > button.ant-btn.ant-dropdown-trigger"))

        self.select_div((By.XPATH,'/html/body/div[6]/div/div/ul/li'),'删除')

    def click_delete_reposts_accept(self):
        '''
        点击批量删除中提示框的确定按钮
        :return: 
        '''
        # /html/body/div[8]/div/div[2]/div/div[2]/div/div/div[2]/button[2]
        self.click((By.XPATH,'/html/body/div[8]/div/div[2]/div/div[2]/div/div/div[2]/button[2]'))

    def click_set_defaule_respost(self, name):
        '''
        点击仓库设置为默认
        :return: 
        '''
        # 获取所有仓库名称
        reposts = self.find_elements((By.XPATH,'//*[@id="app"]/section/section/main/div[2]/div/div/div/div/div[3]/div/div/div/div/div/div/div[2]/table/tbody/tr/td[3]'))

        # 定义一个计数器，来记录目标仓库所在行
        count = 0
        for repost in reposts:
            count += 1

            if repost.text == name:
                # 设置为默认的点击连接
                default_click_element = '//*[@id="app"]/section/section/main/div[2]/div/div/div/div/div[3]/div/div/div/div/div/div/div[2]/table/tbody/tr[%d]/td[11]/span/a[1]' %count

                self.click((By.XPATH,default_click_element))
                #因为只能设置一个默认仓库，所以点击完成之后，需要结束循环
                break

    def click_add_repost(self):
        self.click((By.CSS_SELECTOR,"#app > section > section > main > div:nth-child(3) > div > div > div > div > div.table-operator > button.ant-btn.ant-btn-primary"))

    def input_repost_name(self,name):
        self.send_keys((By.ID,"name"),name)


    def input_repost_addr(self,addr):
        self.send_keys((By.ID,"address"),addr)


    def add_repost(self,name,addr=None,warehousing=None,truckage=None):
        self.click_add_repost()
        self.input_repost_name(name)
        if addr:
            self.input_repost_addr(addr)

    def click_add_repost_accept(self):
        self.click((By.CSS_SELECTOR,"body > div:nth-child(9) > div > div.ant-modal-wrap.ant-modal-cust-warp > div > div.ant-modal-content > div.ant-modal-footer > div > button.ant-btn.ant-btn-primary"))

